﻿using System.Windows;
using Baosight.Platform.Core.UI.Controls;
using System.Collections.Generic;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using System.Windows.Threading;
using System;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCP.UI.PopWindows
{
    public partial class AddMineMonthValidateData : EFViewCmes
    {
        public string isCalculateEnd = "1";
        public string measuringMethod = "1";
        public string mineValidateFormula = "1";
        public string coalDensity = "1.38";
        public string gangueDensity = "2.6";
        public AddMineMonthValidateData(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }


        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            switch (type)
            {
                case "insert":
                    this.fdYear.Value = DateTime.Now.Year;
                    this.fdMonth.Value = DateTime.Now.Month.ToString();
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    base.Mode = OperationMode.Update;
                    BaseTools.EFDataFieldNotEnabled(this.InsertGrid.GetChildObjects<EFDataField>(null, true));
                    Commit.Visibility = Visibility.Collapsed;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick(eServiceName, sender, e);
            return;
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }


        /// <summary>
        /// 工程名称选择改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdUnit_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null && !"".Equals(box.SelectedItem))
            {
                this.fdNo.Value = box.SelectedItem.GetType().GetProperty("fdNo").GetValue(box.SelectedItem, null);
                this.fdDepartment.Value = box.SelectedItem.GetType().GetProperty("fdDepartment").GetValue(box.SelectedItem, null);
                this.fdDepartmentId.Value = box.SelectedItem.GetType().GetProperty("fdDepartmentId").GetValue(box.SelectedItem, null);
            }
        }


        /// <summary>
        /// 施工部门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdDepartmentId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null && !"".Equals(box.SelectedItem))
            {
                this.fdDepartment.Value = box.SelectedItem.GetType().GetProperty("orgName").GetValue(box.SelectedItem, null);
            }

        }

        /// <summary>
        /// 上顺槽终止点计算，以及回采面积
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdUpLength_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            this.fdUpStarPoint_ValueChanged(sender, e);
            GetArea();
        }

        /// <summary>
        /// 下顺槽终止点，以及回采面积
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdDownLength_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            this.fdDownStarPoint_ValueChanged(sender, e);
            GetArea();
        }

        /// <summary>
        /// 计算回采面积
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdExcavateLength_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetArea();
        }

        /// <summary>
        /// 计算矸石量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdGangueThickness_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetGangueSum();
            GetSum();
        }

        /// <summary>
        /// 计算矸石量
        /// </summary>
        private void GetGangueSum()
        {
            double gs = 0.0, md = 2.6, mj = 0.0;
            if (this.fdArea.Value != null)
            {
                double.TryParse(this.fdArea.Value.ToString(), out mj);
            }
            if (this.fdGangueThickness.Value != null)
            {
                double.TryParse(this.fdGangueThickness.Value.ToString(), out gs);
            }
            double.TryParse(gangueDensity, out md);
            this.fdGangueSum.Value = Math.Round(gs * md * mj, 2);
        }

        /// <summary>
        /// 计算验收量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdArea_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetGangueSum();
            GetSum();
        }

        /// <summary>
        /// 计算回采面积
        /// </summary>
        private void GetArea()
        {
            if ("1".Equals(measuringMethod))
            {
                double cc = 0.0, tj1 = 0.0, tj2 = 0.0;
                if (this.fdExcavateLength.Value != null)
                {
                    double.TryParse(this.fdExcavateLength.Value.ToString(), out cc);
                }
                if (this.fdUpLength.Value != null)
                {
                    double.TryParse(this.fdUpLength.Value.ToString(), out tj1);
                }
                if (this.fdDownLength.Value != null)
                {
                    double.TryParse(this.fdDownLength.Value.ToString(), out tj2);
                }
                this.fdArea.Value = Math.Round(cc * ((tj1 + tj2) / 2), 2);
            }
            else if ("0".Equals(measuringMethod))
            {
                double cc = 0.0, tj1 = 0.0;
                if (this.fdExcavateLength.Value != null)
                {
                    double.TryParse(this.fdExcavateLength.Value.ToString(), out cc);
                }
                if (this.fdUpLength.Value != null)
                {
                    double.TryParse(this.fdUpLength.Value.ToString(), out tj1);
                }
                this.fdArea.Value = Math.Round(cc * tj1, 2);
            }
        }

        /// <summary>
        /// 计算验收量
        /// </summary>
        private void GetSum()
        {
            if ("1".Equals(mineValidateFormula))
            {
                double mj = 0.0, cg = 0.0, gsl = 0.0, md = 1.38, gsh = 0.0;
                double.TryParse(coalDensity, out md);
                if (this.fdArea.Value != null)
                {
                    double.TryParse(this.fdArea.Value.ToString(), out mj);
                }
                if (this.fdExcavateHeight.Value != null)
                {
                    double.TryParse(this.fdExcavateHeight.Value.ToString(), out cg);
                }
                if (this.fdGangueThickness.Value != null)
                {
                    double.TryParse(this.fdGangueThickness.Value.ToString(), out gsh);
                }
                if (this.fdGangueSum.Value != null)
                {
                    double.TryParse(this.fdGangueSum.Value.ToString(), out gsl);
                }
                this.fdSum.Value = Math.Round(mj * (cg - gsh) * md + gsl, 2);
            }
            else if ("0".Equals(mineValidateFormula))
            {
                double mj = 0.0, cg = 0.0, md = 1.38;
                double.TryParse(coalDensity, out md);
                if (this.fdArea.Value != null)
                {
                    double.TryParse(this.fdArea.Value.ToString(), out mj);
                }
                if (this.fdExcavateHeight.Value != null)
                {
                    double.TryParse(this.fdExcavateHeight.Value.ToString(), out cg);
                }
                this.fdSum.Value = Math.Round(mj * cg * md, 2);
            }
        }

        /// <summary>
        /// 计算上顺槽终止点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdUpStarPoint_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if ("1".Equals(isCalculateEnd))
            {
                this.fdUpEndPoint.Value = this.fdUpStarPoint.Value;
                double start = 0.0, length = 0.0;
                if (this.fdUpStarLength.Value != null)
                {
                    double.TryParse(this.fdUpStarLength.Value.ToString(), out start);
                }
                if (this.fdUpLength.Value != null)
                {
                    double.TryParse(this.fdUpLength.Value.ToString(), out length);
                }
                this.fdUpEndLength.Value = start - length;
            }
        }

        /// <summary>
        /// 计算下顺槽终止点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdDownStarPoint_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if ("1".Equals(isCalculateEnd))
            {
                this.fdDownEndPoint.Value = this.fdDownStarPoint.Value;
                double start = 0.0, length = 0.0;
                if (this.fdDownStarLength.Value != null)
                {
                    double.TryParse(this.fdDownStarLength.Value.ToString(), out start);
                }
                if (this.fdDownLength.Value != null)
                {
                    double.TryParse(this.fdDownLength.Value.ToString(), out length);
                }
                this.fdDownEndLength.Value = start - length;
            }
        }
    }
}
